// pages/patientCard/index.js
import {
  areaList
} from '../../miniprogram_npm/@vant/area-data/area.js';
import {
  ApiAddaddressList,
  ApiEditaddressList,
  ApiDefaultAddress
} from '../../api/index'
Page({

  /**
   * 页面的初始数据
   */
  data: {
    countdown: 0,
    showpopup: false,
    areaList,
    relationArr: ['本人', '家人'],
    cardTypeArr: ['居民身份证', '护照'],
    nationArr: ['汉族', '回族'],
    opt: {
      area: '湖北省-荆门市-京山市'
    },
    checked: true
  },


  //是否默认邮寄人员
  onChangeSwitch({
    detail
  }) {
    this.setData({
      checked: detail,
    })
    if (detail) {
      ApiDefaultAddress(this.data.opt.id).then(res => {
        if (res.code == 200) {
          this.setData({
            'opt.use': '1'
          })
          wx.showToast({
            title: '设置成功',
          })
        }
      })
    }

  },




  rules(val, message) {
    if (!/^1[3456789]\d{9}$/.test(val)) {
      wx.showToast({
        title: `请输入正确的${message}`,
        icon: 'none'
      });
      return false;
    } else {
      return true
    }
  },


  getCountdown() {
    this.timer = setInterval(_ => {
      const countdown = this.data.countdown - 1
      if (countdown < 1) {
        clearInterval(this.timer)
        this.timer = null
      }
      this.setData({
        countdown
      })
    }, 1000)
  },




  //地址确认
  confirmBtn(v) {
    console.log(v)
    let type = v.currentTarget.dataset.v
    let val = ""
    if (type == 'area') {
      val = v.detail.values
      let value = (val[0].name + "-" + val[1].name + "-" + val[2].name)
      this.setData({
        [`opt.${type}`]: value,
        'showpopup': false
      })
    } else {
      val = v.detail.value
      this.setData({
        [`opt.${type}`]: val,
        'showpopup': false
      })
    }
    console.log(this.data.opt)
  },
  cancelBtn() {
    this.setData({
      'showpopup': false
    })
  },
  onChange(e) {
    const type = e.currentTarget.dataset.type
    const value = e.detail
    this.setData({
      [`opt.${type}`]: value
    })

  },

  //身份证拍照
  handlePhotoClick() {
    this.setData({
      showUploadImage: true
    })
  },


  //保存就诊卡
  addPatientCard() {
    if (this.data.title == '添加邮寄地址') {
      ApiAddaddressList(this.data.opt).then(res => {
        if (res.code == '200') {
          wx.showToast({
            title: "保存成功",
          })
          setTimeout(() => {
            wx.navigateBack()
          }, 500);

        }
      })
    } else {
      delete this.data.opt.created_at
      delete this.data.opt.updated_at
      delete this.data.opt.nameSample
      ApiEditaddressList(this.data.opt.id, this.data.opt).then(res => {
        if (res.code == '200') {
          wx.showToast({
            title: "保存成功",
          })
          setTimeout(() => {
            wx.navigateBack()
          }, 500);

        }
      })
    }


  },

  getDetailArea(e) {
    this.setData({
      showpopup: true,
      showpopupType: e.currentTarget.dataset.v
    })
  },


  /**
   * 生命周期函数--监听页面加载
   */
  onLoad(options) {
    if (options.data) {
      this.setData({
        opt: JSON.parse(options.data),
        title: '编辑邮寄地址'
      })
    } else {
      this.setData({
        title: '添加邮寄地址'
      })
    }
  },

  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady() {

  },

  /**
   * 生命周期函数--监听页面显示
   */
  onShow() {

  },

  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide() {

  },

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload() {

  },

  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh() {

  },

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom() {

  },

  /**
   * 用户点击右上角分享
   */
  onShareAppMessage() {

  }
})